import getopt
import json
import sys
import time
import traceback
from multiprocessing import Pool

import pandas as pd
from es_pandas import es_pandas

from air_web.config.config import config
from air_web.data_platform import mysql_con
from air_web.web_flask.dal.base_dal import EsBaseDal

es_host = config["ES_HOST"]
es = EsBaseDal(es_host)
ep = es_pandas(es_host)


def write_to_es(df, index_name):
    doc_type = index_name + "_doc"
    ep.to_es(
        df,
        index=index_name,
        doc_type=doc_type,
        thread_count=2,
        chunk_size=5000,
        use_index=False,
        request_timeout=60,
    )


def restore_data_structure(df):
    def add_common_columns(row):
        columns = [
            "cons_no",
            "cons_name",
            "data_date",
            "on5",
            "shi",
            "on7",
            "xian",
            "type_code",
            "type_code_sort",
            "real_cons_no",
            "mp_id",
        ]
        return {c: row.eval(c.upper()) for c in columns}

    def add_binary_columns(data):
        binary_columns = [
            "data_time",
            "p_total",
            "p_kt",
            "p_base",
            "p_std_left",
            "p_std_right",
            "is_day_max",
        ]
        return {c: data.get(c.upper()) for c in binary_columns}

    result = []
    for row in df.itertuples():
        tmp_list = []
        json_binary = row.binary
        binary = json.loads(json_binary)

        tmp_acc = add_common_columns(row)

        for tmp_data in binary:
            tmp_list.append(tmp_acc.update(add_binary_columns(tmp_data)))
        result.extend(tmp_list)

    return pd.DataFrame(result)


def sql_form(sql, value=[]):
    return [{"sql": sql, "value": value}]


# 校验任务有没有没生成log的
# end_time为空代表一直执行
# 不同的状态流转都是插入一条新数据
def gen_task_log(
    task_id,
    task_name,
    task_status,
    start_time,
    end_time,
    pid=None,
    node_name=None,
):
    up_sql = "update task_log set is_remove = 1 where is_remove = 0 and task_id = %s and start_time = %s and end_time = %s"
    up_value = [task_id, start_time, end_time]
    sql_list = sql_form(up_sql, up_value)
    sql = "insert into task_log(task_id, task_name, task_status, pid, node_name, start_time, end_time) values(%s, %s, %s, %s, %s, %s, %s)"
    value = [
        task_id,
        task_name,
        task_status,
        pid,
        node_name,
        start_time,
        end_time,
    ]
    sql_list.extend(sql_form(sql, value))
    mysql_con.execute(sql_list)
